<?php
namespace App\Http\Controllers\ConsoleWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\Admin\Auth\AdminAuth;
use App\Http\Controllers\Business\Admin\Common\Common;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;

class IndexController extends WebController
{
    //首页
    public function index() {
        $user = Auth::guard('adminweb')->user();
        $userName = $user['realname'];
        if($this->request->has('login')) {
            //登录以后跳转
            $login = $this->request->get('login');
        } else {
            $login = 0;
        }
        $data = [
            'username' => $userName,
        ];
        if($login) {
            $authCityResult = Common::getAuthCity();
            $cityId = isset($authCityResult['data']['cityid'])?$authCityResult['data']['cityid'] : '1';
            $cityName = isset($authCityResult['data']['cityname'])?$authCityResult['data']['cityname'] : '南昌市';
            return response()->make(view('console.index', $data))->withCookie(cookie('cityid', $cityId))->withCookie(cookie('cityname', $cityName));
        } else {
            return view('console.index', $data);
        }
    }

    //检测用户使用权限
    public function getAdminUserAuth() {
        $adminUser = Auth::guard('adminweb')->user();
        $authJson = $adminUser->auth;
        $authArray = json_decode($authJson, true);
        $data = [];
        foreach($authArray as $list) {
            if(!strstr($list, ":")) {
                array_push($data, $list);
            }
        }
        return $this->responseSuccessData($data);
    }

    //获取该用户下管辖的所有城市, 以及选择城市
    public function getAuthCity() {
        $authCityResult = Common::getAuthCity();
        $allCity = $authCityResult['data'];
        if(Cookie::has('cityid')) {
            $cityId = Cookie::get('cityid');
            $cityName = Cookie::get('cityname');
        } else {
            if(empty($allCity)) {
                return view('errors.404');
            }
            $cityId = $allCity['cityid'];
            $cityName = $allCity['cityname'];
        }
        $data = [
            'allcity' => $allCity['lists'],
            'cityid' => $cityId,
            'cityname' => $cityName
        ];
        return $this->responseSuccessData($data);
    }
    //选择城市
    public function selectCity() {
        if($this->request->has('cityid')) {
            $cityId = $this->request->get('cityid');
            $cityName = $this->request->get('cityname');
            return $this->response->array(['status' => 'success'])->withCookie(cookie('cityid', $cityId))->withCookie(cookie('cityname', $cityName));
        } else {
            $cityId = '';
            $cityName = '';
            return $this->response->array(['status' => 'success'])->withCookie(cookie('cityid', $cityId))->withCookie(cookie('cityname', $cityName));
        }
    }

    //登录
    public function login() {
        return view('console.login');
    }
    //验证登录
    public function submitLoginData() {
        $mobile = $this->request->get('mobile');
        $mobile = trim($mobile);
        $password = $this->request->get('password');
        $password = trim($password);
        $result = AdminAuth::adminLogin($mobile, $password);
        if($result['status'] == 'failed') {
            $data = ['message' => $result['message']];
            return view('console.login', $data);
        } else {
            $userId = $result['data']['userid'];
            Auth::guard('adminweb')->loginUsingId($userId);
            return redirect()->to('/console/index?login=1');
        }
    }
    //忘记密码
    public function forgetPassword() {
        return view('console.forget_password');
    }
    //获取验证码
    public function getCaptcha() {
        $mobile = $this->request->get('mobile');
        $result = AdminAuth::getCode($mobile);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    public function submitForgetPassword() {
        $mobile = $this->request->get('mobile');
        $code = $this->request->get('code');
        $password = $this->request->get('password');
        $passwordConfirm = $this->request->get('password_confirm');
        if($password != $passwordConfirm) {
            $data = ['message'=>'两次密码不一致'];
            return view('console.forget_password', $data);
        }
        $result = AdminAuth::resetPassword($mobile, $code, $password);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //登出
    public function logout() {
        Auth::guard('adminweb')->logout();
        return redirect()->to('/console/login');
    }
    //账号信息
    public function accountInformation() {
        $result = AdminAuth::myUserInfo();
        if($result['data'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];

        return view('console.account.account_information', $data);
    }

    //重置密码
    public function resetPassword() {
        $oldPassword = $this->request->get('oldpassword');
        $newPassword = $this->request->get('newpassword');
        $checkPassword = $this->request->get('checkpassword');
        if($checkPassword != $newPassword) {
            return $this->responseError("两次密码不一致");
        }
        $result = AdminAuth::editPassword($oldPassword, $newPassword);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessMessage($result['message']);
    }
    //
    public function getSmsCode() {
        $mobile = $this->request->get('mobile');
        $type = $this->request->get('type'); //type user表示用户版， shop表示商家版
        $user = Auth::guard('adminweb')->user();
        $roleId = $user['roleid'];
        if(($roleId != '1') && ($roleId != '2') && ($roleId != '3') && ($roleId != '5')) {
            return $this->responseError('无权访问');
        }
        $result = Common::getSmsCode($mobile, $type);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        return $this->responseSuccessData($data);
    }
}